package com.carleasoft.mps.data.mybatisplus.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * MP配置,同时移植了项目中老的配置,兼容老的项目
 * 官网：@see {https://mp.baomidou.com/guide/}
 *
 * @author Alay
 * @date 2021-07-08 22:49
 * @project jmrh
 */
@Slf4j
@Configuration
public class MybatisPlusConfig {

    @PostConstruct
    public void printInfo() {
        log.info("Mybatis-plus 配置文件类 填充配置类准备加载");
    }

    /**
     * MP 新的配置插件配置
     * 官网说明：@see {https://mp.baomidou.com/guide/page.html}
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        /**
         * 乐观锁
         */
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        /**
         * 分页插件
         */
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        /**
         * 防止全表更新与删除
         */
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());

        return interceptor;
    }
}